#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

//命令行参数，命令行输入./test ls -a -l，则argv[]={"./test", "ls", "-a", "-l"};
int main(int argc, char* argv[])
{
    printf("程序正在运行\n");
    pid_t id = fork();
    assert(id != -1);
    if(id == 0)
    {
        sleep(1);
        //int execvp(const char *file, char *const argv[]);
        //第一个参数是要替换的进程。且是p，传入argv[1]，
        execvp(argv[1], &argv[1]);
        
        //execl("/usr/bin/ls", "ls", "-a", "-l", "--color=auto", NULL); 
        //execlp("ls", "ls", "-a", "-l", "--color=auto", NULL);
        //char* argv[]={"ls", "-a", "-l", "--color=auto", NULL};
        //execv("/usr/bin/ls", argv); 
        //execvp("ls", argv);
        //execl("./mybin", "mybin");
        
        //extern char** environ;
        //将指定的环境变量导入到系统中，即environ指向的环境变量表
        //putenv((char*)"MYPATH=19992024");
        //execle("./mybin", "mybin", NULL, environ);
        exit(6);
    }

    int status = 0;
    pid_t ret=waitpid(id, &status, 0);
    if(ret>0)
    {
        printf("等待成功，退出码：%d，信号：%d\n", (status>>8)&0xFF, status&0x7f);
    }
    return 0;
}
